package com.ys.otms.utils.download.impl;

import com.ys.otms.dto.ExportTransportPlanDto;
import com.ys.otms.utils.ListUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.text.SimpleDateFormat;
import java.util.List;


public class TransportDownloadHandler extends DefaultDownloadHandler {

    public String[] getReportHeader() {
        return new String[]{
                "运单编号", "创建日期", "托运人", "托运人联系方式", "货种", "运单状态",
                "船名", "船主姓名", "联系电话",
                "发货地/发货港口", "计划总吨位", "抵达发货地日期", "开始装货时间", "装货完成时间", "受载吨位",
                "目的地/卸货港口", "抵达目的地日期", "开始卸货时间", "卸货完成时间", "实收吨位",
                "航次周期", "单价/Ｔ", "总运价", "已付定金", "结余款项", "装/卸货差/T",
                "调度员", "描述"};
    }

    public Integer[] getReportWidth() {
        return new Integer[]{5000, 5000, 4000, 5000, 4000, 4000,
                5000, 5000, 5000,
                5000, 4000, 5000, 5000, 5000, 4000,
                5000, 5000, 5000, 5000, 4000,
                4000, 3000, 3000, 3000, 4000,
                4000, 3000};
    }

    public void dealReportHeader(HSSFWorkbook wb, HSSFSheet sheet) {
        HSSFCellStyle style1 = wb.createCellStyle();
        style1.setAlignment(HorizontalAlignment.CENTER);
        style1.setVerticalAlignment(VerticalAlignment.CENTER);
        //第一行
        HSSFRow row = sheet.createRow(0);
        //第二行
        CellRangeAddress cra = new CellRangeAddress(1, 1, 0, 27);
        sheet.addMergedRegion(cra);
        HSSFRow row1 = sheet.createRow(1);
        HSSFCell cell1 = row1.createCell(0);
        row1.setHeight((short) 0x240);//设置行高
        cell1.setCellValue("oTMS动态表");
        cell1.setCellStyle(style1);

        style1 = wb.createCellStyle();
        style1.setAlignment(HorizontalAlignment.CENTER);
        style1.setVerticalAlignment(VerticalAlignment.CENTER);
        style1.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style1.setFillForegroundColor(HSSFColor.BROWN.index);
        HSSFFont font = wb.createFont();
        font.setFontHeightInPoints(Short.parseShort("12"));
        font.setBold(true);
        font.setColor(HSSFColor.WHITE.index);
        style1.setFont(font);

        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 5));
        HSSFRow row2 = sheet.createRow(2);
        row2.setHeight((short) 0x180);
        cell1 = row2.createCell(0);
        cell1.setCellValue("货运信息");
        cell1.setCellStyle(style1);

        sheet.addMergedRegion(new CellRangeAddress(2, 2, 6, 8));
        cell1 = row2.createCell(6);
        cell1.setCellValue("船舶信息");
        cell1.setCellStyle(style1);

        sheet.addMergedRegion(new CellRangeAddress(2, 2, 9, 14));
        cell1 = row2.createCell(9);
        cell1.setCellValue("装运信息");
        cell1.setCellStyle(style1);

        sheet.addMergedRegion(new CellRangeAddress(2, 2, 15, 19));
        cell1 = row2.createCell(15);
        cell1.setCellValue("卸货信息");
        cell1.setCellStyle(style1);

        sheet.addMergedRegion(new CellRangeAddress(2, 2, 20, 25));
        cell1 = row2.createCell(20);
        cell1.setCellValue("费用信息");
        cell1.setCellStyle(style1);

        sheet.addMergedRegion(new CellRangeAddress(2, 2, 26, 27));
        cell1 = row2.createCell(26);
        cell1.setCellValue("其他");
        cell1.setCellStyle(style1);

        String[] headers = getReportHeader();
        Integer[] widths = getReportWidth();

        // header
        HSSFRow rowHeader = sheet.createRow(3);

        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setFillForegroundColor(HSSFColor.DARK_GREEN.index);
        HSSFPalette palette = wb.getCustomPalette();
        palette.setColorAtIndex(Short.parseShort("10"), (byte) 0x9B, (byte) 0xBB, (byte) 0x59);
        style.setFillForegroundColor(Short.parseShort("10"));
        style.setFont(font);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = rowHeader.createCell(i);
            cell.setCellValue(headers[i]);
            cell.setCellStyle(style);
            sheet.setColumnWidth(i, i < widths.length ? widths[i] : 6000);
        }
    }

    public void dealReportContent(HSSFWorkbook wb, HSSFSheet sheet, List<Object> dataList) {
        if (ListUtil.isEmpty(dataList)) {
            return;
        }

        for (int i = 0; i < dataList.size(); i++) {
            ExportTransportPlanDto exportTransportPlanDto = (ExportTransportPlanDto) dataList.get(i);
            int index = 0;

            HSSFCellStyle style = wb.createCellStyle();
            style.setAlignment(HorizontalAlignment.CENTER);
            style.setFillPattern(i % 2 == 0 ? FillPatternType.NO_FILL : FillPatternType.SOLID_FOREGROUND);
            HSSFPalette palette = wb.getCustomPalette();
            palette.setColorAtIndex(Short.parseShort("11"), (byte) 0xD7, (byte) 0xE3, (byte) 0xBC);
            style.setFillForegroundColor(Short.parseShort("11"));

            HSSFRow rowContent = sheet.createRow(i + 4);
            //运单编号
            HSSFCell cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getPlanName());

            //创建日期
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(exportTransportPlanDto.getCreateTime()));

            //托运人
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getHuoZhuName());

            //托运人联系方式
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getPhoneNum());

            //货种
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getGoodsName());

            //运单状态
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getTransportState());

            //船名
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getShipName());

            //船主姓名
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getShipOwnerName());

            //联系电话
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getContactPhone());

            //发货地/发货港口
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getOriginalName());

            //计划总吨位
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getPlanTonnage());

            //抵达发货地日期
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getArrivedOriginalTime());

            //开始装货时间
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getStartLoadTime());

            //装货完成时间
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getEndLoadTime());

            //受载吨位
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getSzTonnage());

            //目的地/卸货港口
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getDestinationName());

            //抵达目的地日期
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getArrivedDestinationTime());

            //开始卸货时间
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getStartUploadTime());

            //卸货完成时间
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getEndUploadTime());

            //实收吨位
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getSsTonnage());

            //航次周期
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getZq());

            //单价/Ｔ
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getPrice());

            //总运价
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getTotalPrice());

            //已付定金
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getDeposit());

            //结余款项
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getBalance());

            //装/卸货差/T
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getLUnloadingPoor());

            //调度员
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getDispatcher());

            //描述
            cell = rowContent.createCell(index++);
            cell.setCellStyle(style);
            cell.setCellValue(exportTransportPlanDto.getDescribe());

        }
    }

    public void dealReportSummary(HSSFWorkbook hb, HSSFSheet sheet, List<Object> dataList) {

    }
}
